
#include "audioinfo.h"

using namespace Org;

std::map<string, string> AudioInfo::tag_key_map;

AudioInfo::AudioInfo(const string filepath)
{
    extract_filetype(filepath);
    extract_bitrate(filepath);
    extract_duration(filepath);
    extract_metadata(filepath);
}

/* A naive extension based filetype determiner. */
void AudioInfo::extract_filetype(const string filepath)
{
    audio_type = AudioInfo::mp3;
}

void AudioInfo::extract_metadata(const string filepath)
{
    TagLib::FileRef file_ref(filepath.c_str());

    if(!file_ref.isNull() && file_ref.tag()) {
        TagLib::Tag *tag = file_ref.tag();

        metadata.insert(make_pair("YEAR", AudioInfo::stringify(tag->year())));
        metadata.insert(make_pair("ARTIST", tag->artist().to8Bit()));
        metadata.insert(make_pair("ALBUM", tag->album().to8Bit()));
        metadata.insert(make_pair("TITLE", tag->title().to8Bit()));
        metadata.insert(make_pair("COMMENT", tag->comment().to8Bit()));
        metadata.insert(make_pair("TRACKNUMBER", AudioInfo::stringify(tag->track())));
        metadata.insert(make_pair("GENRE", tag->genre().to8Bit()));
    }
}

void AudioInfo::extract_duration(const string filepath)
{
    #if NOT_YET_IMPLEMENTED
        cout << "NOT_YET_IMPLEMENTED: AudioInfo::extract_duration" << endl;
        duration = 0;
    #endif
}

void AudioInfo::extract_bitrate(const string filepath)
{
    #if NOT_YET_IMPLEMENTED
        cout << "NOT_YET_IMPLEMENTED: AudioInfo::extract_bitrate" << endl;
        bitrate = 0;
    #endif
}
